// pages/zixun/courseAll/courseAll.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
      zxUrl: "http://localhost:8080/file/downLoad?fileName=",
      pageNum: 1,
      jjList: [],
      total: 0,
      isLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.getData();
  },

  /**
   * 获取数据
   */
  getData() {
      const that = this;
      if (that.data.isLoading) {
          return;
      }
      that.setData({
          isLoading: true
      });
      wx.showLoading({
          title: '正在加载中...',
          icon: 'loading'
      });
      wx.request({
          url: 'http://localhost:8080/course/selectAll',
          data: {
              pageNum: that.data.pageNum,
              pageSize: 10
          },
          success(resp) {
              const result = resp.data;
              if (result.code === 1) {
                  that.updateList(result.data.list, result.data.total);
              } else {
                  wx.showToast({
                      title: '数据获取失败，错误码：' + result.code
                  });
              }
          },
          fail() {
              wx.showToast({
                  title: '网络请求失败，请检查网络连接'
              });
          },
          complete() {
              wx.hideLoading();
              wx.stopPullDownRefresh();
              that.setData({
                  isLoading: false
              });
          }
      });
  },

  /**
   * 更新列表数据
   */
  updateList(list, total) {
      const that = this;
      if (that.data.pageNum === 1) {
          that.setData({
              jjList: list,
              total: total
          });
      } else {
          that.setData({
              jjList: that.data.jjList.concat(list),
              total: total
          });
      }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
      this.setData({
          pageNum: 1,
          jjList: []
      });
      this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
      if (this.data.isLoading) {
          return;
      }
      if (this.data.pageNum * 5 >= this.data.total) {
          wx.showToast({
              title: '没有更多数据了....',
          });
          return;
      }
      this.setData({
          pageNum: this.data.pageNum + 1
      });
      this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
});